package cn.wolfcode.shop.cloud.mapper;

import cn.wolfcode.shop.cloud.domain.Good;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.ArrayList;
import java.util.List;


@Mapper
public interface GoodsMapper {
    //select * from t_goods where id in (1,2)

    /**
     * 两个参数 class和方法
     * 配置好这两个参数，那么它会去指定类的指定方法找到方法的返回值，返回值就是它最终要执行的sql语句
     * @param ids
     * @return
     */
    @SelectProvider(type = SelectQuery.class,method = "queryByIds")
    List<Good> queryByIds(@Param("ids") ArrayList<Long> ids);


    class SelectQuery {
        public String queryByIds(@Param("ids") ArrayList<Long> ids){
            StringBuilder sb = new StringBuilder(100);
            sb.append("select * from t_goods");

            if(ids!=null && ids.size()>0){
                sb.append(" where id in ( ");
                for (int i = 0; i < ids.size(); i++) {
                    if(i != 0){
                        sb.append(",");
                    }
                    sb.append(ids.get(i));
                }
                sb.append(" ) ");
            }
            return sb.toString();
        }
    }
}
